var cCount=1;
var totalpage = 0;
var pageCount=5;
var pagecap=5;
var doSub=false;
var doWhichPage=false;
var subchild = 0;
var boo=false;

function setPage(x)
{
	//alert("x:"+x+" pageCount:"+pageCount+" totalpage:"+totalpage);
	var inserDiv="";
	if(x<=1) {
		x=1;
		if(pageCount>totalpage)
			inserDiv=getTempStr(x,totalpage);	
		else
			inserDiv=getTempStr(x,pageCount); 
	}
	else {	
		if((x+pageCount)<totalpage) {
			inserDiv=getTempStr(x,pageCount+x);
		}
		else if((x+pageCount)==totalpage) {
			inserDiv=getTempStr(x,totalpage);
		}
		else {
			inserDiv=getTempStr(x,totalpage);
		}
	}
	
	return inserDiv;
}

function getTempStr(x,y)
{
	if(x<0) x=0;
	var tempX="";
	
	if (navigator.appName.indexOf("Explorer")>0) {
	tempX+="<P align='center' style='color:#000000'><label id='pagefirst' onclick='firstpage()' onmouseout=mouseout(event.srcElement) onmouseover=mouseover(event.srcElement)>首页</label>&nbsp;";
	tempX+="<label id='pageprev' onclick='gotopage()' onmouseout=mouseout(event.srcElement) onmouseover=mouseover(event.srcElement)>上一页&lt;</label>&nbsp;";
	
	for(var i=x;i<=y;i++)
	  tempX+="<A  onclick=gotoCurPage(event.srcElement.innerText)>"+(i)+"</A>&nbsp;";
	  tempX+="<label id='pagenext' onclick='gotopage()' onmouseout=mouseout(event.srcElement) onmouseover=mouseover(event.srcElement)>&gt;下一页</label>";
	  tempX+=" <label id='pageend' name='pageend' onclick=endpage() onmouseout=mouseout(event.srcElement) onmouseover=mouseover(event.srcElement)>末页</label></P>";	
	}else{
	
		tempX+="<P align='center' style='color:#000000;'><label id='pagefirst' style='cursor:pointer;' onclick='firstpage()' onmouseout=mouseout(event.target) onmouseover=mouseover(event.target)>首页</label>&nbsp;";
	tempX+="<label id='pageprev' style='cursor:pointer;' onclick='gotopage(event)' onmouseout=mouseout(event.target) onmouseover=mouseover(event.target)>上一页&lt;</label>&nbsp;";
	
	for(var i=x;i<=y;i++)
	  tempX+="<A style='cursor:pointer;' onclick=gotoCurPage(event.target.textContent)>"+(i)+"</A>&nbsp;";
	  tempX+="<label id='pagenext' style='cursor:pointer;' onclick='gotopage(event)' onmouseout=mouseout(event.target) onmouseover=mouseover(event.target)>&gt;下一页</label>";
	  tempX+=" <label id='pageend' style='cursor:pointer;' name='pageend' onclick=endpage() onmouseout=mouseout(event.target) onmouseover=mouseover(event.target)>末页</label></P>";	
	
	
	}
	
	return tempX;
}


function mouseClickA(x)
{
	var linkA=document.getElementById("mylink").getElementsByTagName("A");
	
    
	for(var i=0;i<linkA.length;i++)
	{
		if (navigator.appName.indexOf("Explorer")>0) {
				
			if(linkA[i].innerText==x) { 
				linkA[i].style.color="red";
			}
			else {
				linkA[i].style.color="blue";
			}
		}
		else {
			if(linkA[i].textContent==x) { 
				linkA[i].style.color="red";
			}
			else {
				linkA[i].style.color="blue";
			}
		}
	}	
}


function mouseout(x)
{
	x.style.backgroundColor="white";	
}

function mouseover(x)
{
	x.style.backgroundColor="86e3eb";
}

function gotoCurPage(x)
{
	cCount=new Number(x);
	if(cCount < 1) 
		cCount = 1;
		
	mouseClickA(x)
	
	if(x < totalpage)
		changePage(x);
	else
		changePage(totalpage); 
		
	if((cCount % pageCount) != 0) {
		//cCount++;
	}
	else {
		cCount++;
		boo=true;
	}
	
	doSub=true;
}

function endpage()
{
	if(cCount != totalpage) 
		cCount = totalpage;
	changePage(totalpage);
		
	if(!doWhichPage) {
		if((Math.round(totalpage / pageCount)*pageCount ) < totalpage)
			setPage(Math.round(totalpage / pageCount) * pageCount);
		else
			setPage(Math.round(totalpage / pageCount) * pageCount-10);
	}
	else              
		setPage(Math.round(totalpage / pageCount)*pageCount);  
		
	boo=true;
	findLinkText(cCount)  ;
	doSub=false;           
	doWhichPage=false;
}

function firstpage()
{
	if(cCount != 1) 
		cCount = 1;
    setPage(cCount);
    findLinkText(cCount);
	changePage(1);
    doSub=false;           
    boo=true;
} 

function gotopage()
{	  
	var doFlg=window.event.srcElement.id;
	
	if(doFlg=="pagenext") {
		if(boo && !doSub) {
			//cCount = cCount + 1;
			findLinkText(cCount);
		}            	
		else {
			
			findLinkText(cCount);
		}
		
		if((cCount + 1) <= totalpage) {			
			cCount++;  
			changePage(cCount);		
			if(cCount%pageCount==1 && cCount!=1) {
				setPage(cCount); 
				findLinkText(cCount-1);
			}       
		}
		else {
			doWhichPage=true;
			endpage();
		}
	
		doSub=false;           
		boo=false;
	}
	
	if(doFlg=="pageprev") {
		if(cCount != totalpage) { 
			if(!boo && !doSub ) {
				cCount = cCount-1;
				findLinkText(cCount);
			}

			else {
				//if(!doSub) { 
					cCount--;
					findLinkText(cCount);
				//}
				//else {
					//cCount = cCount-2;
					//findLinkText(cCount);
				//} 	
			}
		}
		else {
			//if(!doSub) { 
				cCount--;
				findLinkText(cCount);
			//}
			//else {
				
				//cCount=cCount-2;
				//alert(cCount);
				//findLinkText(cCount);
			//} 	
		}
		
		if(cCount <= 1) 
			cCount = 1;
		boo=true;
		changePage(cCount);
		findLinkText(cCount); 
		if(cCount%pageCount==pageCount-1) {
			if(cCount>0) {	
				setPage(cCount-pageCount+2);                    
				findLinkText(cCount);
			}
			else
				findLinkText(0);
		}
		
		doSub=false;           
	}

}

function findLinkText(x)
{ 
	var linkA=document.getElementById("mylink").getElementsByTagName("A");
	
	for(var i=0;i<linkA.length;i++)
	{
		if (navigator.appName.indexOf("Explorer")>0) {
				
			if(linkA[i].innerText==x) { 
				linkA[i].style.color="red";
			}
			else {
				linkA[i].style.color="blue";
			}
		}
		else {
			if(linkA[i].textContent==x) { 
				linkA[i].style.color="red";
			}
			else {
				linkA[i].style.color="blue";
			}
		}
	}
}